package com.gxa.eshop.admin.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;

@Configuration
public class ActiveMQConfig
{

    @Value("${spring.activemq.broker-url}")
    private String brokerUrl;

    @Value("${spring.activemq.user}")
    private String username;

    @Value("${spring.activemq.password}")
    private String password;

//    @Value("${spring.activemq.queue-name}")
    @Value("${queue-name}")
    private String queueName;

//    @Value("${spring.activemq.topic-name}")
//    private String topicName;

    @Bean(name = "queue")
    public Queue queue() {
        return new ActiveMQQueue(queueName);
    }


//    @Bean(name = "topic")
//    public Topic topic() {
//        return new ActiveMQTopic(topicName);
//    }

    @Bean
    public ConnectionFactory connectionFactory(){
        return new ActiveMQConnectionFactory(username, password, brokerUrl);
    }

    @Bean
    public JmsMessagingTemplate jmsMessageTemplate()
    { JmsMessagingTemplate jmsMessagingTemplate=new JmsMessagingTemplate(connectionFactory());
        return jmsMessagingTemplate;
    }
//    @Bean(name = "simpleJmsTemplate")
//    public JmsTemplate getJmsTemplate(){
//        JmsTemplate jmsTemplate=new JmsTemplate(connectionFactory());
//        jmsTemplate.setSessionTransacted(false);
//        jmsTemplate.getSessionAcknowledgeMode();
//        return  jmsTemplate;
//    }
    @Bean("simpleJmsTemplate")
    public JmsTemplate getJmsTemplate(){
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory());
        //开启手动签收
        jmsTemplate.setSessionAcknowledgeMode(4);
        //关闭事务
        jmsTemplate.setSessionTransacted(false);
        return jmsTemplate;
    }


}